package com.minxi.jsp.dao.impl;

import com.minxi.jsp.dao.UserDao;
import com.minxi.jsp.javabean.UserInfo;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class UserDaoImpl implements UserDao {
    @Override
    public Boolean add(UserInfo userInfo) {
        Boolean flag = false;
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        //2.请求数据库 JDBC
        try {
            //1.注册驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            //2.获取连接
            String url = "jdbc:mysql://localhost:3306/student?serverTimezone=Asia/Shanghai";
            String user = "root";
            String pwd = "123456";
            connection = DriverManager.getConnection(url, user, pwd);
            //3.编辑SQL语句 (预编译的执行对象)
            String sql = "insert into user_info(user_name,user_pwd,user_phone,user_sex)values(?,?,?,?);";
            //得到执行对象
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1,userInfo.getUserName());
            preparedStatement.setString(2,userInfo.getPassWord());
            preparedStatement.setString(3,userInfo.getUserPhone());
            preparedStatement.setInt(4,userInfo.getUserSex());
            int rs = preparedStatement.executeUpdate();
            if(rs > 0){
                flag = true;
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                if(null != preparedStatement){
                    preparedStatement.close();
                }
                if(null != connection){
                    connection.close();
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return flag;
    }

    @Override
    public List<UserInfo> list() {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        //集合泛型
        List<UserInfo> list = new ArrayList<>();
        //2.请求数据库 JDBC
        try {
            //1.注册驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            //2.获取连接
            String url = "jdbc:mysql://localhost:3306/student?serverTimezone=Asia/Shanghai";
            String user = "root";
            String pwd = "123456";
            connection = DriverManager.getConnection(url, user, pwd);
            //3.编辑SQL语句 (预编译的执行对象)
            String sql = "select id,user_name,user_age,user_sex,user_phone from user_info;";
            //得到执行对象
            preparedStatement = connection.prepareStatement(sql);
            ResultSet resultSet = preparedStatement.executeQuery();
            //数据处理
            while (resultSet.next()){
                int id = resultSet.getInt("id");
                String userName = resultSet.getString("user_name");
                int userAge = resultSet.getInt("user_age");
                int userSex = resultSet.getInt("user_sex");
                String userPhone = resultSet.getString("user_phone");

                //怎么来封装这些字段数据？ 使用字符串比较麻烦，不好在页面展示
                UserInfo userInfo = new UserInfo();
                userInfo.setId(Long.parseLong(String.valueOf(id)));
                userInfo.setUserName(userName);
                userInfo.setUserAge(userAge);
                userInfo.setUserSex(userSex);
                userInfo.setUserPhone(userPhone);
                list.add(userInfo);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                if(null != preparedStatement){
                    preparedStatement.close();
                }
                if(null != connection){
                    connection.close();
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return list;
    }
}
